<template>
<!-- 用户购买vip -->
  <div class='consumerPayVip'>
    <el-dialog title="修改用户VIP购买活动" :visible.sync="isShow" center :before-close="close">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-position="right" label-width="200px">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="start_time">
          <el-date-picker
            v-model="form.start_time"
            type="datetime"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="end_time">
          <el-date-picker
            v-model="form.end_time"
            type="datetime"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="过期时间(天)" prop="expire_time">
          <el-input v-model="form.expire_time"></el-input>
        </el-form-item>
        <el-form-item label="铜牌VIP购买价格(元)" prop="vip1">
          <el-input v-model="form.vip1"></el-input>
        </el-form-item>
        <el-form-item label="银牌VIP购买价格(元)" prop="vip2">
          <el-input v-model="form.vip2"></el-input>
        </el-form-item>
        <el-form-item label="金牌VIP购买价格(元)" prop="vip3">
          <el-input v-model="form.vip3"></el-input>
        </el-form-item>
        <el-form-item label="钻石VIP购买价格(元)" prop="vip4">
          <el-input v-model="form.vip4"></el-input>
        </el-form-item>
        <el-form-item label="活动下线" >
          <el-checkbox v-model="form.offline" disabled>是否下线活动</el-checkbox>
        </el-form-item>
      </el-form>
      <div slot="footer" >
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submint">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    var validateMoney = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入金额'))
      } else {
        if (!Number(value)) {
          callback(new Error('请输入正确的金额'));
        } else if (value <= 0) {
          callback(new Error('金额必须大于0'));
        } else {
          callback()
        }
      }
    }
    return {
      form: {
        start_time: '',
        end_time: '',
        expire_time: '',
        vip1: null,
        vip2: null,
        vip3: null,
        vip4: null,
        name: '',
        offline: false
      },
      isShow: false,
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'change' }
        ],
        start_time: [
          { required: true, message: '请选择开始时间', trigger: 'change' }
        ],
        end_time: [
          { required: true, message: '请输入结束原因', trigger: 'change' }
        ],
        expire_time: [
          { required: true, message: '请输入过期时间', trigger: 'change' }
        ],
        vip1: [
          { required: true, validator: validateMoney, trigger: 'change' }
        ],
        vip2: [
          { required: true, validator: validateMoney, trigger: 'change' }
        ],
        vip3: [
          { required: true, validator: validateMoney, trigger: 'change' }
        ],
        vip4: [
          { required: true, validator: validateMoney, trigger: 'change' }
        ],
      },
      id: '',
    }
  },
  methods: {
    show (data) {

      // let condition = JSON.parse(data.condition)
      let condition = data.condition
      this.id = data.id
      this.form.name = data.name
      this.form.start_time = condition.start_time
      this.form.end_time = condition.end_time
      this.form.expire_time = condition.expire_time
      this.form.vip1 = condition.vip1
      this.form.vip2 = condition.vip2
      this.form.vip3 = condition.vip3
      this.form.vip4 = condition.vip4
      this.isShow = true
    },
    submint() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.submintPost()
        } else {
          return false;
        }
      })
    },
    submintPost() {
      let params = {
        name: this.form.name,
        activity: this.id
      }
      let condition = {}
      for( let key in this.form) {
        condition[key] = this.form[key]
      }
      delete condition.name
      params.condition = JSON.stringify(condition)
      // 下线活动
      if (this.form.offline) {
        params.status = 6
      }
      this.$axios.put( `api/activity/${this.id}`, params).then(res => {
        if (res.data.code == 200 ) {
          this.$message({
            message: '修改活动成功',
            type: 'success'
          })
          this.$emit('getData')
          this.close()
        } else if (res.data.code == 400) {
          this.$message.error(res.data.message)
          this.close()
        }

      })
    },
    close() {
      this.$refs['ruleForm'].resetFields()
      this.isShow = false
    }
  }
}
</script>

<style lang='less' type='text/css'>
  .consumerPayVip {
    .el-date-editor {
      width: 100%;
    }
  }

</style>
